package com.paddyi.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.paddyi.commons.http.HttpResult;
import com.paddyi.commons.page.MyPage;
import com.paddyi.model.Resource;
import com.paddyi.model.Role;
import com.paddyi.model.RoleResource;
import com.paddyi.service.IResourceService;
import com.paddyi.service.IRoleResourceService;
import com.paddyi.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static com.alibaba.druid.sql.visitor.SQLEvalVisitorUtils.eq;

/**
 * 角色 (Role)
 *
 * @author paddy
 * @since 2019-04-26 04:34:10
 */
@RestController
@RequestMapping("role")
public class RoleController extends BaseController {

    @Autowired
    private IRoleService roleService;
    @Autowired
    private IResourceService resourceService;
    @Autowired
    private IRoleResourceService roleResourceService;

    /**
     * 添加角色
     *
     * @param record
     * @return
     */
    @PostMapping(value = "/add")
    public HttpResult add(@RequestBody Role record) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("name",record.getName());
        Map r = roleService.getMap(queryWrapper);
        if(r != null){
            return HttpResult.error("角色名称重复");
        }
        Long userId = getUserId();
        Date date = new Date();
        record.setCreateUserId(userId);
        record.setCreateTime(date);
        record.setUpdateUserId(userId);
        record.setUpdateTime(date);
        return HttpResult.ok(roleService.save(record));
    }

    /**
     * 修改角色
     *
     * @param record
     * @return
     */
    @PostMapping(value = "/edit")
    public HttpResult edit(@RequestBody Role record) {
        record.setUpdateTime(new Date());
        record.setUpdateUserId(getUserId());
        return HttpResult.ok(roleService.updateById(record));
    }

    /**
     * 删除角色
     *
     * @param records
     * @return
     */
    @PostMapping(value = "/delete")
    public HttpResult delete(@RequestBody List<Role> records) {
        List list = new ArrayList();
        for(Role or : records){
            list.add(or.getId());
        }
        return HttpResult.ok(roleService.removeByIds(list));
    }

    /**
     * 基础分页查询,条件查询只支持等于，如果想要更高级的功能，比如大于小于等等，请自己写吧
     *
     * @param page
     * @return
     */
    @PostMapping(value = "/findPage")
    public HttpResult findPage(@RequestBody MyPage<Role> page) {
        return HttpResult.ok(roleService.page(page));
    }

    /**
     * 查询所有
     *
     * @return
     */
    @GetMapping(value = "/findAll")
    public HttpResult findAll() {
        return HttpResult.ok(roleService.list());
    }

    /**
     * 根据主键查询
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/findById")
    public HttpResult findById(@RequestParam Long id) {
        return HttpResult.ok(roleService.getById(id));
    }

    /**
     * 根据RoleID查询权限
     *
     * @param roleId
     * @return
     */
    @GetMapping(value = "/findResourceByRoleId")
    public HttpResult findResourceByRoleId( Long roleId) {

        return HttpResult.ok(resourceService.findResourceByRoleId(roleId));
    }

    /**
     * 保存角色权限
     *
     * @param roleMenus
     * @return
     */
    @PostMapping(value = "/saveRoleMenus")
    public HttpResult saveRoleMenus(@RequestBody List<RoleResource> roleMenus) {
        Long roleId = roleMenus.get(0).getRoleId();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("role_id",roleId);
        roleResourceService.remove(queryWrapper);
        return HttpResult.ok(roleResourceService.saveBatch(roleMenus));
    }
}
